package com.hll.filter;


import com.Constants;
import com.hll.model.User;
import com.hll.util.StringUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(filterName = "SessionFilter")
public class SessionFilter implements Filter {

    /**不赋空值则可能会报空指针异常，*/
    private String notNeedLoginPages = "";
    protected FilterConfig filterConfig;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        if (filterConfig.getInitParameter("notNeedLoginPages") != null) {
            notNeedLoginPages = filterConfig.getInitParameter("notNeedLoginPages");
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        /**获取URI*/
        String uri = request.getRequestURI();

        /**去掉contextPath*/
        String requestedUri = uri.substring(request.getContextPath().length() + 1);
        System.out.println("requestedUri=" + requestedUri);

        String[] pages = notNeedLoginPages.split(",");

        /**是否允许经过*/
        boolean isAllow = false;

        /**看requestedUri是否符合  "/ "和"/login.action",是则允许进入filter*/
        for (String page : pages) {
            if (page.equals(requestedUri)) {
                isAllow = true;
                break;
            }
        }
        if (isAllow) {
            filterChain.doFilter(request, response);
        } else {
            /**否则，取session，看user是否为空，若为空，则说明用户数据已被清除，返回登录界面
             *若不为空则说明已经存在用户session说明用户已经登录，则允许进入过滤器，执行对应访问操作，
             * 如后续访问的myBook.action等
             */
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute(Constants.USER);

            if (user == null) {
                //判断是不是get方法
                if(request.getMethod().toLowerCase().equals("get")) {

                    /**获取参数，即url问号?后面的参数*/
                    String queryString = request.getQueryString();

                    /**产生异常的网页url去掉contextPath之后赋值给go*/
                    String go = requestedUri;
                    if(!StringUtil.isEmpty(queryString)) {
                        go = go + "#" + request.getQueryString();
                    }
                    /**重定向！即让浏览器重新get访问，此时会进入login.action的doget()方法，对应语句(1)*/
                    response.sendRedirect(request.getContextPath() + "/login.action?go=" + go);
                }  else {
                    //不是get方法，正常进入myBook.action
                    response.sendRedirect(request.getContextPath() + "/login.action");
                }

                /**这里表明session不为空，说明已经登陆过了，允许进入*/
            } else {
                filterChain.doFilter(request, response);
            }
        }

    }

    @Override
    public void destroy() {
        this.notNeedLoginPages = null;
    }

}